package com.wyg.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.page.Page;
import com.wyg.pojo.User;
import com.wyg.util.HibernateSessionFactory;

public class UserDao {
	
	public void registe(User user){
		
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		
		session.save(user);
		trans.commit();
		
	}
	
	public User login(String phone,String password){
		
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		//查询满足条件的数据
		Query query = session.createQuery("from User u where u.phoneNumber=:phone and u.pwd=:password");
		query.setParameter("phone", phone);
		query.setParameter("password", password);
		User user = (User) query.uniqueResult();
		trans.commit();
		return user;
	}
	//用户总人数
	public int getTotalCount(){
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		String hqlCount = "select count(*) from User";
		Query querycount = session.createQuery(hqlCount);
		int totalCount = Integer.parseInt(querycount.uniqueResult().toString());
		trans.commit();
		return totalCount;
	}
	//分页查询用户
	public List<User> findPage(Page page){
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		String hql="from User";
		Query querypage = session.createQuery(hql);
		querypage.setMaxResults(page.getPagesize());
		querypage.setFirstResult(page.getStartrow());
		List<User> list = querypage.list();
		trans.commit();
		return list;
	}
	
	//所有用户
	public List queryAllUser(){
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		
		Query query = session.createQuery("from User");
		List list = query.list();
		trans.commit();
		return list;
	}
	
	public User queryPhone(String phone){
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
	
		Query query = session.createQuery("from User u where u.phoneNumber=:phone");
		query.setParameter("phone", phone);
		User user =(User) query.uniqueResult();
		trans.commit();
		return user;
	}
	
	//删除用户
	public void deleteUser(int id){
		
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		User u = (User) session.get(User.class, id);
		session.delete(u);
		trans.commit();
	}
	//查询单个用户
	public User queryUser(int id){
		Session session = HibernateSessionFactory.getSession();
		Transaction t = session.beginTransaction();
		
		User user = (User) session.get(User.class, id);
		t.commit();
		return user;
	}
	
	//更改用户信息
	public void changeUserInfo(int id,String phone,String name ,String ipid, String address, String abcType){
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		
		User user = (User) session.get(User.class, id);
		user.setName(name);
		user.setPhoneNumber(phone);
		user.setAbcType(abcType);
		user.setAddress(address);
		user.setIpid(ipid);
		user.setAddress(address);
		trans.commit();
	}
	
	//重置密码
	public void initPwd(int id){
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		
		User user = (User) session.get(User.class, id);
		user.setPwd("000000");
		trans.commit();
	}
	
	//用户修改密码
	public void changePwd(int id, String pwd){
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		
		User user = (User) session.get(User.class, id);
		user.setPwd(pwd);
		trans.commit();
		
	}
	
	//更改激活状态
	public void changeStatus(int id , String status){
		Session session = HibernateSessionFactory.getSession();
		Transaction trans = session.beginTransaction();
		
		User user = (User) session.get(User.class, id);
		user.setStatus(status);
		trans.commit();
		
	}
	
	
	
//	public static void main(String[] args) {
//		UserDao ud = new UserDao();
//		for(char i='a';i<'z';i++){
//			System.out.println(i);
//			User user = new User("188888888"+i, "123456", "王小"+i, "1306"+i, "sjz", "c2","未激活");
//			ud.registe(user);
//		}
////		User user = new User("admin","admin","管理员","1306admin","石家庄","A","已激活");
////		ud.registe(user);
//	}

}
